<?php

class OrderSaleController extends BaseController
{
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Order;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Order']))
		{
			$model->attributes=$_POST['Order'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->zo_id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Order']))
		{
			$model->attributes=$_POST['Order'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->zo_id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * 买家未付款订单，卖家可做逻辑删除
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDeleteOrder($id)
	{
		$model = $this->loadModel($id);
		$model->zo_del = 4;//双方删除
		$model->zo_status = 2;//作费订单
		
		if($order->save()){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}	
	}
	
	/**
	 * 线下支付，确认收款
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionSureUnlinePay($id)
	{
		$model = $this->loadModel($id);
		$model->zo_pay_status = 2;//支付状态：2已支付',
		$model->zo_pay_time = time();//确认付款时间
	
		if($model->save()){
			//成功跳转
			$this->redirect(array('saleorder'));
		}
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Order');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Order('search');
		$model->unsetAttributes();  // clear any default values
		$model->zm_id = Company::model()->getUserId();//限制当前商家
		if(isset($_GET['Order']))
			$model->attributes=$_GET['Order'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Order::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='order-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	//查看已卖出的宝贝
	public function actionSaleorder()
	{
		$model = new Order();
		$model->unsetAttributes();
		//取头前商家ID
		$model->cmpUserId = Company::getCmpUserId();
		$this->render('sale_order',array(
					'model'=>$model,
		));
	}

	//修改价格
	public function actionUpdatePrice()
	{
		$id = trim($_GET['id']);
		$order = Order::model()->findByPk($id);
		
		if(isset($_POST['pro_price'])){
			$pro_price = $_POST['pro_price'];
			$zo_amount = 0;
			foreach($pro_price as $key=>$val){
				$ordProduct = OrdProduct::model()->findByPk($key);
				$ordProduct->zop_real_price = $val;
				$ordProduct->save();
				$zo_amount += $val;
			}
			//print_r($zo_amount);exit;
			$order->zo_real_freight = (int)$_POST['freight']; //订单运费
			$order->zo_real_amount = $zo_amount;			//实付商品总金额
			$order->zo_amount = $zo_amount+$order->zo_real_freight;//实付订单总额
			$order->zo_discount = $order->zo_payable_amount-$zo_amount;	//订单优惠
			if($order->save()){
				//成功跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}
		
		if(!empty($order)){
			$data['freight'] = $order->zo_real_freight;
			$data['sumprice'] = $order->zo_real_amount;
			$data['orderid'] = $order->zo_id;
			$data['reality'] = $order->zo_real_amount-$order->zo_real_freight;
			foreach($order->orderProduct as $key=>$val){
				$data['product'][] = array(
									'title'=>$val->zop_title,
									'price'=>$val->zop_price,
									'nums'=>$val->zop_nums,
									'sumprice'=>$val->zop_real_price,
									'spec'=>unserialize($val->zop_spe),
									'zop_id'=>$val->zop_id,
									);
			}
			echo json_encode($data);
		}
	}
	
	/*
	 * 查看订单详细
	 * */
	public function actionOrderDetail($id){
		$model = Order::model()->findByPk($id);
		$this->render('order_detail',array(
				'model'=>$model,
		));
	}
}
